import http from "@/axios/index.js";
import { handleSuccess } from "@/api/index.js";
import { ElMessage } from "element-plus"; // 使用 Element Plus 的消息组件

export const deptApi = {
    // 创建部门
    create: (data) =>
        http.post('/api/dept/create', data)
            .then(res => handleSuccess(res, '部门创建成功'))
            .catch(error => {
                ElMessage.error(error.message || '部门创建失败');
                throw error;
            }),

    // 修改部门信息
    update: (data) =>
        http.put(`/api/dept/update`, data)
            .then(res => handleSuccess(res, '部门信息修改成功'))
            .catch(error => {
                ElMessage.error(error.message || '部门信息修改失败');
                throw error;
            }),

    // 删除部门
    delete: (id) =>
        http.delete(`/api/dept/delete/${id}`)
            .then(res => handleSuccess(res, '部门删除成功'))
            .catch(error => {
                ElMessage.error(error.message || '部门删除失败');
                throw error;
            }),

    // 获取部门列表
    getList: (query) =>
        http.get('/api/dept/list', { params: query })
            .then(res => handleSuccess(res, '部门列表获取成功'))
            .catch(error => {
                ElMessage.error(error.message || '部门列表获取失败');
                throw error;
            }),

    // 获取部门详情
    getDetail: (id) =>
        http.get(`/api/dept/detail/${id}`)
            .then(res => handleSuccess(res, '部门详情获取成功'))
            .catch(error => {
                ElMessage.error(error.message || '部门详情获取失败');
                throw error;
            }),

    // 获取部门树结构
    getTree: () =>
        http.get('/api/dept/tree')
            .then(res => handleSuccess(res, '部门树结构获取成功'))
            .catch(error => {
                ElMessage.error(error.message || '部门树结构获取失败');
                throw error;
            }),

    // 部门用户列表
    getUserList: (params) =>
        http.get(`/api/dept/users`, { params })
            .then(res => handleSuccess(res, '部门用户列表获取成功'))
            .catch(error => {
                ElMessage.error(error.message || '部门用户列表获取失败');
                throw error;
            }),

    // 部门结构调整
    structure: (data) =>
        http.post('/api/dept/structure', data)
            .then(res => handleSuccess(res, '部门结构调整成功'))
            .catch(error => {
                ElMessage.error(error.message || '部门结构调整失败');
                throw error;
            })
};
